package com.chuhe.lottery.service.adapter;

import com.chuhe.lottery.dao.PrizePoolsDao;
import com.chuhe.lottery.dao.WinnersDao;
import com.chuhe.lottery.domain.entity.PrizePools;
import com.chuhe.lottery.domain.entity.dto.WinnerDto;
import com.chuhe.lottery.domain.entity.vo.ResultResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 楚河
 */
@Component
public class ResultAdapter {
    private final WinnersDao winnersDao;
    private final PrizePoolsDao prizePoolsDao;

    @Autowired
    public ResultAdapter(WinnersDao winnersDao, PrizePoolsDao prizePoolsDao) {
        this.winnersDao = winnersDao;
        this.prizePoolsDao = prizePoolsDao;
    }

    /**
     * 结果返回组装
     * @Author: 楚河
     */
    public HashMap<String, Object> buildResultAdapter(HashMap<String, Object> result,long runtime) {
        result.put("结果","抽奖完成！！！");
        result.put("总耗时",runtime);

        Integer winnerSumNum = winnersDao.selectAllCount();

        ArrayList<ResultResp> awardsResp = new ArrayList<>();
        List<PrizePools> poolsInfo = prizePoolsDao.selectAll();
        awardsResp = poolsInfo.stream()
                .map(pool -> {
                    ResultResp resp = new ResultResp();
                    resp.setId(pool.getId());
                    resp.setDescribe(pool.getDescribes());
                    resp.setCount(pool.getCounts());
                    return resp;
                })
                .collect(Collectors.toCollection(ArrayList::new));

        ArrayList<WinnerDto> winnersResp = new ArrayList<>();
        WinnerDto winnersInfo = winnersDao.selectWinnerInfo();
        winnersResp.add(winnersInfo);

        result.put("lotteryNumber",winnerSumNum);
        result.put("awards",awardsResp);
        result.put("winnersInfo",winnersResp);
        return result;
    }

}
